#!/bin/sh --login

umask 002

lapsdataroot=$1    # Example: /lfs0/projects/hmtb/dwr_domains/laps_psd   
hhmm=$2            # Runtime needed (e.g. 01:00)
delay=$3           # Delay in hours for the modeltime with respect to model_cycle_time (e.g. 12.0)                    
remote_dataroot=$4 # Remote dataroot to send output to (without machine name)
domain_name=$5     # domain name (from the "on-the-fly") page
rsync_qsub=$6      # do rsync via qsub [optional: yes,no - under construction]          

model_cycle_time=`@PERL@ @prefix@/etc/read_nl.pl -d $lapsdataroot -n nest7grid.parms -v model_cycle_time`
n_verif_steps=`@PERL@ @prefix@/etc/read_nl.pl -d $lapsdataroot -n nest7grid.parms -v n_verif_steps`

echo "model_cycle_time = $model_cycle_time"
echo "n_verif_steps = $n_verif_steps"

if test "$name" != "none"; then                               # (e.g. ewp0)
    run=$name
elif test "$physics" != "none" && test "$lbc" != "none"; then # (e.g. wrf-fer-gep0)
    run=$model\-$physics\-$lbc
elif test "$physics" != "none" && test "$lbc" == "none"; then # (e.g. wrf-fer)
    run=$model\-$physics
elif test "$physics" == "none" && test "$lbc" != "none"; then # (e.g. wrf-gep0)
    run=$model\-$lbc
elif test "$physics" == "none" && test "$lbc" == "none"; then # (e.g. wrf)
    run=$model\-$lbc
fi

rm -f $lapsdataroot/log/qsub_verif_pbs.log.`date +\%H\%M`
log=$lapsdataroot/log/run_qsub_verif.log.`date +\%H\%M`

# Build qsub script
script=$lapsdataroot/log/qsub_verif.sh
echo "#!/bin/sh --login"           > $script
echo "#PBS -l procs=1"            >> $script
echo "#PBS -l walltime=$hhmm:00"  >> $script
echo "#PBS -l vmem=4500M"         >> $script
echo "#PBS -A dlaps"              >> $script
echo "#PBS -N vrf_$domain_name"   >> $script
#echo "#PBS -S /bin/sh"            >> $script
#echo "#PBS -cwd"                  >> $script
echo "#PBS -j oe"                 >> $script
echo "#PBS -o $lapsdataroot/log/qsub_verif_pbs.log.`date +\%H\%M`"          >> $script
echo " "                          >> $script
echo "umask 002"                  >> $script

echo " "                                                        >> $script
echo " echo 'Running this verif_fcst_driver.csh command...'"    >> $script

#@prefix@/etc/verif/verif_fcst_driver.csh /pan1/projects/dlaps/analysis/laps_ci 21600.0  5.75  900 20 > /pan1/projects/dlaps/analysis/laps_ci/log/verif_fcst_driver.log.`date +\%H` 2>&

echo " echo '@CSH@ @prefix@/etc/verif/verif_fcst_driver.csh $lapsdataroot $model_cycle_time $delay $domain_name $n_verif_steps'                                                     "   >> $script
echo "       @CSH@ @prefix@/etc/verif/verif_fcst_driver.csh $lapsdataroot $model_cycle_time $delay $domain_name $n_verif_steps > $lapsdataroot/log/verif_fcst_driver.log.`date +\%H`"   >> $script

echo " "                                                        >> $script
echo " "                                                        >> $script
echo " touch $log.done"                                         >> $script
echo " exit 0"                                                  >> $script

echo " "
echo " Running qsub script contained in $script...."
cat $script
echo " "
echo " using this command..."

if test "$remote_dataroot" == ""; then   
    echo "qsub $script > $log 2>&1"
          qsub $script > $log 2>&1

else
    rm -f $log.done

    echo "@prefix@/etc/qsub_wait $script $log.done"
          @prefix@/etc/qsub_wait $script $log.done

    echo " "
    echo "Making image montages via ssh"
    echo ssh dlaps-ms1.fsl.noaa.gov /usr/bin/perl /usr/nfs/lapb/parallel/laps/etc/followup_verif.pl -r $remote_dataroot -d $domain_name -y $delay -t $n_verif_steps 
         ssh dlaps-ms1.fsl.noaa.gov /usr/bin/perl /usr/nfs/lapb/parallel/laps/etc/followup_verif.pl -r $remote_dataroot -d $domain_name -y $delay -t $n_verif_steps  

    rsync_qsub=yes 

    if test "$rsync_qsub" == "yes"; then   

#       Rsync the verification data to remote dataroot
        echo "@prefix@/etc/rsync_out.sh $lapsdataroot dlaps-ms1.fsl.noaa.gov:$remote_dataroot qsub 01:00 8"
              @prefix@/etc/rsync_out.sh $lapsdataroot dlaps-ms1.fsl.noaa.gov:$remote_dataroot qsub 01:00 8

    else

        date -u                                                                                  
        echo " " 
        echo "Start copy of verif directories (under construction)"

        echo "rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/lapsprd/verif/* --exclude='REF/cont' --exclude='REF/hist' $REMOTE_DATA_ROOT/lapsprd/verif"
              rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/lapsprd/verif/* --exclude='REF/cont' --exclude='REF/hist' $REMOTE_DATA_ROOT/lapsprd/verif 

        echo "rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/log/load.png                             $REMOTE_DATA_ROOT/log"
              rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/log/load.png                             $REMOTE_DATA_ROOT/log 

        echo "rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/log/cloud_fcst.png                       $REMOTE_DATA_ROOT/log"
              rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/log/cloud_fcst.png                       $REMOTE_DATA_ROOT/log 

        echo "rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/time/modelvtime.dat                      $REMOTE_DATA_ROOT/time"
              rsync -rlptgvvz $RSH $DELETE $LOCAL_DATA_ROOT/time/modelvtime.dat                      $REMOTE_DATA_ROOT/time 

        echo " "                                            
        date -u                                                                                  

    fi

fi

echo "run_qsub_verif.sh completed..."


